home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ov143b.zip / OVVBUF.C < prev    next >
C/C++ Source or Header  |  1993-01-04  |  12KB  |  311 lines

  1. /* 008  23-May-87  ovvbuf.c
  2.  
  3.         Buffer management routines for ovview.c
  4.  
  5.         Copyright (c) 1986,1987 by Blue Sky Software.  All rights reserved.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include "ov.h"
  11. #include "overr.h"
  12.  
  13. #define MAX_VBUF 8                     /* allow up to 8 in memory buffers */
  14. #define VBUF_LEN 16*1024               /* a buffer is 16K - make a power of 2 */
  15.  
  16. typedef struct {                       /* define the View BUFfer DEScriptior */
  17.    int buflen;
  18.    long bufoff;
  19.    char far *bufp;
  20. } VBUF_DES;
  21.  
  22. static int vidx;                       /* current VBUF descriptor in use     */
  23. static int vlast;                      /* last VBUF descriptor in use        */
  24. static int num_vbuf;                   /* highest buffer that can be alloc'd */
  25. static VBUF_DES vbufd[MAX_VBUF];       /* the arrary of VBUF descriptors     */
  26. static long bufoff;                    /* offset in file of current buffer   */
  27. static unsigned int buflen;            /* length of data in current buffer   */
  28. unsigned char far *bufp;               /* begining of data in buffer         */
  29. unsigned char far *curp;               /* current char location in buffer    */
  30. unsigned char far *endp;               /* end of current buffer              */
  31.  
  32. /*global*/  int ALTCALL vbuf_init(int );
  33. /*global*/  int ALTCALL vbuf_free(void);
  34. /*global*/  unsigned long ALTCALL vtell(void);
  35. /*global*/  int ALTCALL vseek(int ,long );
  36. /*global*/  int ALTCALL vnextch(int );
  37. /*global*/  int ALTCALL vprevch(int );
  38. static  int ALTCALL pagefwd(int ,long );
  39. static  int ALTCALL pagebck(int ,long );
  40. static  int ALTCALL vswitch(int );
  41.  
  42.  
  43. /*****************************************************************************
  44.                           V B U F _ I N I T
  45.  *****************************************************************************/
  46.  
  47. int ALTCALL
  48. vbuf_init(fh)          /* initialize the buffer system */
  49. int fh;
  50. {
  51.    vlast = -1;                         /* set indexes, etc to force an  */
  52.    num_vbuf = MAX_VBUF;                /* so the initial read loads the */
  53.    pagefwd(fh,0L);                     /* first buffer from the file    */
  54.    vswitch(0);                         /* make the 1st buffer active    */
  55.    curp = bufp;                        /* at the 1st char in 1st buffer */
  56. }
  57.  
  58.  
  59. /*****************************************************************************
  60.                           V B U F _ F R E E
  61.  *****************************************************************************/
  62.  
  63. int ALTCALL
  64. vbuf_free() {          /* release all memory buffers */
  65.  
  66.    register int i;
  67.    register VBUF_DES *vp;
  68.  
  69.    for (i = 0, vp = vbufd; i <= vlast; i++, vp++) {  /* release all buffers */
  70.       free_f(vp->bufp);
  71.       vp->bufp = NULL;
  72.    }
  73. }
  74.  
  75.  
  76. /****************************************************************************
  77.                               V T E L L
  78.  ****************************************************************************/
  79.  
  80. unsigned long ALTCALL
  81. vtell() {              /* return file offset of current character */
  82.  
  83.    return(bufoff + (curp - bufp));
  84. }
  85.  
  86.  
  87. /*****************************************************************************
  88.                                V S E E K
  89.  *****************************************************************************/
  90.  
  91. int ALTCALL
  92. vseek(fh,offset)       /* seek to specified offset in file/buffers */
  93. int fh;
  94. long offset;
  95. {
  96.    register int i;
  97.    long readoff, labs();
  98.    register VBUF_DES *vp;
  99.  
  100.    /* "seek" is easy if offset is in current buffer - normal case I hope */
  101.  
  102.    if (offset >= bufoff && offset < bufoff + VBUF_LEN) {
  103.  
  104.       curp = bufp + (offset - bufoff);
  105.       return;
  106.  
  107.    } else {    /* not in current buffer, is it in any buffer? */
  108.  
  109.       for (i = 0, vp = vbufd; i <= vlast; i++, vp++)
  110.          if (offset >= vp->bufoff && offset < vp->bufoff + VBUF_LEN) {
  111.             vswitch(i);                        /* switch to buffer with */
  112.             curp = bufp + (offset - bufoff);   /* wanted offset */
  113.             return;
  114.          }
  115.       }
  116.  
  117.    /* offset location isn't in memory, do it the old fashion way, read it */
  118.  
  119.    readoff = offset & ~((long)(VBUF_LEN-1));   /* where to read from  */
  120.  
  121.    /* special case if seeking what would be the next or prev buffer, we
  122.       keep as much buffered in memory as possible */
  123.  
  124.    if (labs(readoff - bufoff) == VBUF_LEN) {   /* reading next or prev block? */
  125.  
  126.       if (readoff > bufoff) {                  /* next block? */
  127.          pagefwd(fh,readoff);                  /* swap in next buffer */
  128.          vswitch(vidx+1);                      /* make it active */
  129.       } else {                                 /* previous block */
  130.          pagebck(fh,readoff);                  /* swap in prev buffer */
  131.          vswitch(vidx-1);                      /* make it active */
  132.       }
  133.  
  134.    } else {            /* not reading next or prev buffer */
  135.  
  136.       /* seeking to some random position in file, discard all current buffers
  137.          and start over from new position */
  138.  
  139.       vidx = vlast = 0;                        /* discard all buffers */
  140.       vbufd[0].bufoff = bufoff = readoff;      /* where to read from  */
  141.       bufp = vbufd[0].bufp;                    /* where to read to    */
  142.       l_seek(fh,readoff);
  143.       vbufd[0].buflen = buflen = readbuf(fh,bufp,VBUF_LEN);
  144.       endp = bufp + buflen;
  145.    }
  146.  
  147.   curp = bufp + (offset - bufoff);             /* got buffer, goto offset */
  148.   return;
  149. }
  150.  
  151.  
  152. /****************************************************************************
  153.                               V N E X T C H
  154.  ****************************************************************************/
  155.  
  156. int ALTCALL
  157. vnextch(fh)            /* return next character from file/buffers */
  158. int fh;
  159. {
  160.    /************** Following code is duplicated by some callers **************/
  161.  
  162.    if (curp < endp)            /* the normal case is just to return */
  163.       return(*curp++);         /*  the next character in the buffer */
  164.  
  165.    /**************************************************************************/
  166.  
  167.    if (buflen < VBUF_LEN)      /* only last buffer can be < VBUF_LEN */
  168.       return(EOF);
  169.  
  170.    if (vidx == vlast)                          /* read more from file? */
  171.       if (pagefwd(fh,bufoff+VBUF_LEN) == EOF)  /* try paging into the file */
  172.          return(EOF);                          /* watch for EOF */
  173.  
  174.    vswitch(vidx+1);                    /* switch to next buffer  */
  175.    curp = bufp;                        /* at start of buffer */
  176.  
  177.    return(*curp++);                    /* return 1st char in it */
  178. }
  179.  
  180.  
  181. /****************************************************************************
  182.                               V P R E V C H
  183.  ****************************************************************************/
  184.  
  185. int ALTCALL
  186. vprevch(fh)            /* return previous character from file/buffers */
  187. int fh;
  188. {
  189.    /************** Following code is duplicated by some callers **************/
  190.  
  191.    if (curp > bufp)            /* the normal case is just to return */
  192.       return(*--curp);         /*  the prev character in the buffer */
  193.  
  194.    /**************************************************************************/
  195.  
  196.    if (bufoff == 0)            /* at top of file? (offset == 0) */
  197.       return(EOF);
  198.  
  199.    if (vidx == 0)                      /* is this the oldest buffer? */
  200.       pagebck(fh,bufoff-VBUF_LEN);     /*   if so, page backward a buffer */
  201.  
  202.    vswitch(vidx-1);                    /* switch to prev buffer  */
  203.    curp = endp;                        /* at end of buffer */
  204.  
  205.    return(*--curp);                    /* return last char in it */
  206. }
  207.  
  208.  
  209. /*****************************************************************************
  210.                              P A G E F W D
  211.  *****************************************************************************/
  212.  
  213. static int ALTCALL
  214. pagefwd(fh,offset)     /* page forward into the file being read */
  215. int fh;
  216. long offset;
  217. {
  218.    int i;
  219.    register VBUF_DES *vp;
  220.    char far *bp, far *malloc_f();
  221.  
  222.    /* bump the index of the last buffer used, check if all buffer
  223.       descriptors are used, if so reuse the oldest, otherwise try
  224.       to allocate another buffer and use that */
  225.  
  226. getbuf:                /* look ma, a label for a goto! */
  227.  
  228.    if (vlast == num_vbuf-1) {          /* all buffer descriptors used? */
  229.  
  230.       bp = vbufd[0].bufp;        /* remember where buffer is */
  231.       for (i = 0; i < num_vbuf-1; i++) /* throw away oldest buffer by */
  232.          vbufd[i] = vbufd[i+1];        /*  moving others down over it */
  233.       vbufd[vlast].bufp = bp;    /* reuse oldest buffer */
  234.       vidx--;                          /* descriptors were shifted 1 */
  235.  
  236.    } else {            /* try to allocate another buffer */
  237.  
  238.       vlast++;                         /* move to the next descriptor */
  239.  
  240.       /* if we haven't already done so, allocate another buffer for the
  241.          descriptor, failing that use a reduced number of buffers.  We
  242.          need to be able to alloc at least 1 */
  243.  
  244.       if (vbufd[vlast].bufp == NULL)
  245.          if ((vbufd[vlast].bufp = malloc_f(VBUF_LEN)) == NULL) {
  246.             if (vlast == 0)
  247.                show_error(0,NO_MEM,1,"No memory for view buffer");
  248.             num_vbuf = vlast--;   /* can't alloc another buffer, operate with */
  249.             goto getbuf;          /*  a reduced number (minimum 1) of buffers */
  250.          }
  251.    }
  252.  
  253.    /* a descriptor/buffer is ready, fill it with data from the file */
  254.  
  255.    vp = &vbufd[vlast];                         /* speed things up */
  256.    l_seek(fh,vp->bufoff = offset);             /* where to read from */
  257.    vp->buflen = readbuf(fh,vp->bufp,VBUF_LEN);
  258.  
  259.    if (vp->buflen == 0)                   /* very unlikley, but maybe EOF */
  260.       return(EOF);
  261.    else
  262.       return(1);
  263. }
  264.  
  265.  
  266. /*****************************************************************************
  267.                              P A G E B C K
  268.  *****************************************************************************/
  269.  
  270. static int ALTCALL
  271. pagebck(fh,offset)     /* page backward into the file being read */
  272. int fh;
  273. long offset;
  274. {
  275.    char far *bp;
  276.    register int i;
  277.  
  278.    /* reuse the "newest" buffer (last one in descriptor array) */
  279.  
  280.    bp = vbufd[vlast].bufp;             /* remember where buffer is */
  281.    for (i = vlast; i > 0; i--)         /* throw away newest buffer by */
  282.       vbufd[i] = vbufd[i-1];           /*  moving others over it */
  283.  
  284.    vbufd[0].bufp = bp;                 /* reuse newest buffer */
  285.    vidx++;                             /* descriptors were shifted 1 */
  286.  
  287.    /* a descriptor/buffer is ready, fill it with data from the file */
  288.  
  289.    l_seek(fh,vbufd[0].bufoff = offset);        /* where to read from */
  290.    vbufd[0].buflen = readbuf(fh,bp,VBUF_LEN);
  291. }
  292.  
  293.  
  294. /*****************************************************************************
  295.                              V S W I T C H
  296.  *****************************************************************************/
  297.  
  298. static int ALTCALL
  299. vswitch(idx)           /* make buffer idx the current buffer */
  300. int idx;
  301. {
  302.    register VBUF_DES *vp;
  303.  
  304.    vp = &vbufd[vidx = idx];    /* assign vidx, address of vidx descriptor */
  305.  
  306.    bufp = vp->bufp;            /* buffer location */
  307.    bufoff = vp->bufoff;        /* file offset of buffer */
  308.    buflen = vp->buflen;        /* len of data in buffer */
  309.    endp = bufp + buflen;       /* 1 past the last valid data byte in buffer */
  310. }
  311.